package com.web.fsc.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.web.fsc.dao.IPaperDAO;
import com.web.fsc.domain.Paper;
import com.web.fsc.service.IPaperService;

@Service
@Cacheable
public class PaperService implements IPaperService {

	@Autowired
	private IPaperDAO paperDao;
	@Autowired
	private HttpServletResponse response;

	@Override
	public void add(MultipartFile file, Paper paper) {
		String filepath = "E:/web/workspace/FSC/WebContent/upload/";
		try {
			file.transferTo(new File(filepath, file.getOriginalFilename()));
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		paper.setPath(filepath + file.getOriginalFilename());
		paper.setTopic(file.getOriginalFilename());
		paperDao.save(paper);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Paper paper = paperDao.findById(Paper.class, id);
		File file = new File(paper.getPath());
		if (file.exists())
			file.delete();
		paperDao.delete(Paper.class,id);
	}

	@Override
	public List<Paper> searchByTopic(String topic) {
		// TODO Auto-generated method stub
		List<Paper> list = paperDao.findByTopicLike(topic);
		return list;
	}

	@Override
	public List<Paper> listByCategory(String category) {
		// TODO Auto-generated method stub
		return paperDao.findByCategory(category);
	}

	@Override
	public List<Paper> listAll() {
		// TODO Auto-generated method stub
		return paperDao.findAll(Paper.class);
	}

	@Override
	public void download(long fileid) {
		// TODO Auto-generated method stub
		Paper paper = paperDao.findById(Paper.class, fileid);
		String fileName = paper.getTopic();
		File file=new File(paper.getPath());
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		try {
			String path = "E:/web/workspace/FSC/WebContent/upload";
			InputStream inputStream = new FileInputStream(new File(path + File.separator + fileName));
			response.reset(); 
	        // 设置response的Header 
	        response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1"));  //转码之后下载的文件不会出现中文乱码
	        response.addHeader("Content-Length", "" + file.length()); 
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}

			os.close();

			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<Paper> listBySystem(String category,String system) {
		// TODO Auto-generated method stub
		return paperDao.findBySystem(category,system);
	}

	@Override
	public List<Paper> searchByAuthor(String author) {
		// TODO Auto-generated method stub
		return paperDao.findByAuthorLike(author);
	}

	@Override
	public List<Paper> searchByKey_word(String key_word) {
		// TODO Auto-generated method stub
		return paperDao.findByKey_wordLike(key_word);
	}

	@Override
	public List<Paper> searchBySummary(String summary) {
		// TODO Auto-generated method stub
		return paperDao.findBySummaryLike(summary);
	}

	@Override
	public List<Paper> search(String key) {
		// TODO Auto-generated method stub
		return paperDao.ByTopicOrKeyWordOrSummary(key);
	}
}
